<?php

namespace plugin\coleopenapi\app\queue\redis;

use plugin\coleopenapi\app\model\OpenapiRequestLog;
use support\Log;
use Throwable;
use Webman\RedisQueue\Consumer;

class RequestLogEvent implements Consumer
{
    // 要消费的队列名
    public $queue = 'openapi-request-log';

    // 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'default';

    // 消费
    public function consume($data)
    {
        try {
            OpenapiRequestLog::insert([
                "date" => $data['date'],
                "time" => $data['time'],
                "request_id" => $data['request_id'],
                "ip" => $data['ip'],
                "transfer" => $data['transfer'],
                "cost_time" => $data['cost_time'],
                "success" => $data['success'],
                "code" => $data['code'],
                "method" => $data['method'],
                "app_key" => $data['app_key'],
                "details" => $data['details'],
            ]);
        } catch (Throwable $exception) {
            Log::error(RequestLogEvent::class . " insert error " . $exception->getMessage());
        }
    }
}
